<?php
/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2020/5/2
 * Time: 17:36
 */

namespace app\api\model;


use app\common\library\helper;
use app\common\model\BaseModel;
use think\Log;
use think\Request;

class CommissionLog extends BaseModel
{

    public function load($userId, \think\Request $request)
    {

        $page = $this->getPage($request);
        return ['rows' => $this->where(['user_id' => $userId])->order('create_time desc')
            ->page($page)->select(),
            'length' => ceil($this->where(['user_id' => $userId])->count() / 30)
        ];
    }

    public function getAgentCalculatePage($user, $input, Request $request)
    {
        $filter = ['cl.commission_type' => 'order', 'u.ranking' => ['like', $user['ranking'] . "%"]];
        $filter = $this->getCalFilter($filter, $input);
        $list = CommissionLog::alias('cl')->join('order o', 'o.order_id=cl.relate_id')
            ->join('user u', 'u.user_id=cl.user_id')
            ->where($filter)
            ->group('cl.user_id')
            ->field('u.*, count(1) as total_order_count, sum(o.pay_price) as total_order_pay, sum(cl.money) as total_commission')
            ->select();
        $totals = ['total_order_count' => 0, 'total_order_pay' => 0, 'total_commission' => 0];
        $agents = helper::getAgents();
        foreach ($list as &$li) {
            $totals['total_order_count'] += $li['total_order_count'];
            $totals['total_order_pay'] += $li['total_order_pay'];
            $totals['total_commission'] += $li['total_commission'];
            $li['level_name'] = $agents[$li['agent_level']]['name'];
        }
        $totals['total_commission'] = round($totals['total_commission'], 2);
        return ['list' => $list, 'totals' => $totals];
    }

    public function getAgentSales($user, $input, Request $request)
    {
        $filter = ['cl.commission_type' => 'order', 'cl.user_id' => $user->user_id];
        $filter = $this->getCalFilter($filter, $input);
        $list = CommissionLog::alias('cl')
            ->join('order o', 'o.order_id=cl.relate_id', 'left')
            ->join('user creator', 'creator.user_id=cl.creator_id', 'left')
            ->where($filter)
            ->field('o.pay_price, o.order_id, cl.create_time, cl.commission, cl.relate_id,cl.price, cl.money, o.order_no, creator.nickName, creator.avatarUrl')
            ->order("o.create_time desc")
            ->select();
        $totals = ['total_order_count' => 0, 'total_order_pay' => 0, 'total_commission' => 0];
        $agents = helper::getAgents();
        $user['level_name'] = $agents[$user->agent_level]['name'];
        Log::record('list -->' . json_encode($list));
        foreach ($list as &$li) {
            $totals['total_order_count'] += 1;
            $totals['total_order_pay'] += $li['pay_price'];
            $totals['total_commission'] += $li['money'];
        }
        $totals['total_commission'] = round($totals['total_commission'], 2);
        return ['list' => $list, 'totals' => $totals, 'user' => $user];
    }

    private function getCalFilter($filter, $input)
    {
        Log::record("[params] " . json_encode($input));
        $searchType = $input['searchType'];
        $years = explode(",", $input['years']);
        $year = str_replace('年', '', $years[$input['yearIndex']]);
        $filter['cl.year'] = $year;
        if (isset($input['level']))
            $filter['u.agent_level'] = $input['level'];
        if ($searchType == 'month') {
            //按月
            $month = $input['monthIndex'] + 1;

            $filter['cl.month'] = $month;

        } elseif ($searchType == 'season') {
            $seasonIndex = $input['seasonIndex'];
            if (!$seasonIndex) {
                $filter['cl.month'] = ['between', [1, 3]];
            } elseif ($seasonIndex == 1) {
                $filter['cl.month'] = ['between', [4, 6]];
            } elseif ($seasonIndex == 2) {
                $filter['cl.month'] = ['between', [7, 9]];
            } elseif ($seasonIndex == 3) {
                $filter['cl.month'] = ['between', [10, 12]];
            }
        }
        return $filter;
    }
}
